var webpack             = require('webpack');
var ExtractTextPlugin   = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin   = require('html-webpack-plugin');

// 环境变量配置，dev / online
var WEBPACK_ENV         = process.env.WEBPACK_ENV || 'dev';

// 获取html-webpack-plugin参数的方法 
var getHtmlConfig = function(name, title){
    return {
        template    : './src/view/' + name + '.html',
        filename    : 'view/' + name + '.html',
        favicon     : './favicon.ico',
        title       : title,
        inject      : true,
        hash        : true,
        chunks      : ['common', name]
    };
};
// webpack config
var config = {
	//指定开发模式
    mode : 'dev' === WEBPACK_ENV ? 'development' : 'production',
   //入口文件，指定打包入口
    entry: {
        'common'            : './src/page/common/index.js',
        'index'             : './src/page/index/index.js',
        'list'              : './src/page/list/index.js',
        'detail'            : './src/page/detail/index.js',
        'cart'              : './src/page/cart/index.js',
        'order-confirm'     : './src/page/order-confirm/index.js',
        'order-list'        : './src/page/order-list/index.js',
        'order-detail'      : './src/page/order-detail/index.js',
        'payment'           : './src/page/payment/index.js',
        'user-login'        : './src/page/user-login/index.js',
        'user-register'     : './src/page/user-register/index.js',
        'user-pass-reset'   : './src/page/user-pass-reset/index.js',
        'user-center'       : './src/page/user-center/index.js',
        'user-center-update': './src/page/user-center-update/index.js',
        'user-pass-update'  : './src/page/user-pass-update/index.js',
        'result'            : './src/page/result/index.js',
        'about'             : './src/page/about/index.js',
		'paper'             : './src/page/paper/index.js',
		'test-list'         : './src/page/test-list/index.js',
		'become-tutor'      : './src/page/become-tutor/index.js'
    },
	//输出文件
    output : {
        //webpack4中默认打包在/dist路径下
        publicPath  :  WEBPACK_ENV === 'online' ? '//s.jiajiao.space/jiajiao_fe/dist/' : '/dist/',
        filename    : 'js/[name].js'
    },
    externals : {
        'jquery' : 'window.jQuery'
    },
    module : {
	rules:[
			// css文件的处理			
			{
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            // 模板文件的处理
            {
                test: /\.string$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        minimize : true,
                        removeAttributeQuotes : false
                    }
                }
            },
            // 图片的配置
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            //图片小于2kb的按base64打包
                            limit: 2048,
                            name: 'resource/[name].[ext]'
                        }
                    }
                ]
            },
            // 字体图标的配置
            {
                test: /\.(eot|svg|ttf|woff|woff2|otf)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                            name: 'resource/[name].[ext]'
                        }
                    }
                ]
            }
        ]
    },
    resolve : {
        alias : {
            node_modules    : __dirname + '/node_modules',
            util            : __dirname + '/src/util',
            page            : __dirname + '/src/page',
            service         : __dirname + '/src/service',
            image           : __dirname + '/src/image'
        }
    },
    /* 
    * 【新增】：webpack4里面移除了commonChunksPulgin插件，放在了config.optimization里面
    */
   //公共打包文件，当俩个文件超过30k一样时，会将公共部分打包在common.js文件中
    optimization:{
        runtimeChunk: false,
        splitChunks: {
            cacheGroups: {
                common: {
                    name: "common",
                    chunks: "all",
                    minChunks: 2
                }
            }
        }
    },
    plugins: [
        // 把css单独打包到文件里
        new ExtractTextPlugin("css/[name].css"),
        // html模板的处理
        new HtmlWebpackPlugin(getHtmlConfig('index', '首页')),
        new HtmlWebpackPlugin(getHtmlConfig('list', '教员列表')),
        new HtmlWebpackPlugin(getHtmlConfig('detail', '教员详情')),
        new HtmlWebpackPlugin(getHtmlConfig('cart', '购物车')),
        new HtmlWebpackPlugin(getHtmlConfig('order-confirm', '订单确认')),
        new HtmlWebpackPlugin(getHtmlConfig('order-list', '订单列表')),
        new HtmlWebpackPlugin(getHtmlConfig('order-detail', '订单详情')),
        new HtmlWebpackPlugin(getHtmlConfig('payment', '订单支付')),
        new HtmlWebpackPlugin(getHtmlConfig('user-login', '用户登录')),
        new HtmlWebpackPlugin(getHtmlConfig('user-register', '用户注册')),
        new HtmlWebpackPlugin(getHtmlConfig('user-pass-reset', '找回密码')),
        new HtmlWebpackPlugin(getHtmlConfig('user-center', '个人中心')),
        new HtmlWebpackPlugin(getHtmlConfig('user-center-update', '修改个人信息')),
        new HtmlWebpackPlugin(getHtmlConfig('user-pass-update', '修改密码')),
        new HtmlWebpackPlugin(getHtmlConfig('result', '操作结果')),
        new HtmlWebpackPlugin(getHtmlConfig('about', '资讯中心')),
		new HtmlWebpackPlugin(getHtmlConfig('paper', '试题系统')),
		new HtmlWebpackPlugin(getHtmlConfig('test-list', '分数详情')),
		new HtmlWebpackPlugin(getHtmlConfig('become-tutor', '申请成为教员'))
    ],
   //webpack4自带的代理服务器，解决跨域问题
    devServer: {
        port: 8088,
        inline: true,
		disableHostCheck: true,
        proxy : {
            '**/*.do' : {
                target: 'http://localhost:8080/',
				//开启跨域
                changeOrigin : true
            }
        }
    }
};
module.exports = config;